package com.wd.live.platform;

import java.util.LinkedList;
import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectors;
import us.codecraft.webmagic.selector.XpathSelector;

public class ZhanqiRepoPageProcessor extends AbstractProcessor {

	// 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
	 private Pipeline pipeline;
	  public ZhanqiRepoPageProcessor(Pipeline pipeline){
		  this.pipeline = pipeline;
	  }
	  private ZhanqiRepoPageProcessor(){
	  }
	public void process(Page page) {

		List<String> list = page.getHtml().xpath("//ul[@id='hotList']/li/html()").all();
		List<String[]> result = new LinkedList<String[]>();
		for (String str : list) {
			String[] array = new String[4];
			XpathSelector xpathSelector = Selectors.xpath("//span[@class='game-name dv']/html()");
			String gameName = new Html(xpathSelector.selectList(str), false).toString();
			array[0] = gameName;

			xpathSelector = Selectors.xpath("//span[@class='anchor anchor-to-cut dv']/html()");
			String userName = new Html(xpathSelector.selectList(str), false).toString();
			array[1] = userName;

			xpathSelector = Selectors.xpath("//span[@class='views']/span[@class='dv']/html()");
			String total = new Html(xpathSelector.selectList(str), false).toString();
			array[2] = total;

			array[3] = "战旗";
			
			result.add(array);
		}

		page.putField("listDate", result);
	}

	public Site getSite() {

		return site;
	}

	public void execute() {

		/*
		 * List<String> urls = new ArrayList<String>(); urls.add(
		 * "http://www.huya.com/index.php?m=Live&do=ajaxAllLiveByPage&page=4&pageNum=1"
		 * ); // urls.add(
		 * "http://www.huya.com/index.php?m=Live&do=ajaxAllLiveByPage&page=4&pageNum=2"
		 * ); // urls.add(
		 * "http://www.huya.com/index.php?m=Live&do=ajaxAllLiveByPage&page=4&pageNum=3"
		 * ); // urls.add(
		 * "http://www.huya.com/index.php?m=Live&do=ajaxAllLiveByPage&page=4&pageNum=4"
		 * ); // urls.add(
		 * "http://www.huya.com/index.php?m=Live&do=ajaxAllLiveByPage&page=4&pageNum=5"
		 * );
		 * 
		 * Spider.create(new
		 * ZhanqiRepoPageProcessor()).startUrls(urls).addPipeline(new
		 * MyFilePipeline("D:\\222")) // 开启5个线程抓取 .thread(5) // 启动爬虫 .run();
		 */

		Spider.create(new ZhanqiRepoPageProcessor()).addUrl("http://www.zhanqi.tv/lives").addPipeline(pipeline)
				// 开启5个线程抓取
				.thread(5)
				// 启动爬虫
				.run();
	}
}